<?php
namespace App\Service\Item\Fxotary\Admin;

use App\Service\Item\Fxotary\BaseService as Service;
use Illuminate\Support\Facades\DB;

class ActionService extends Service
{
    //菜单

    /**
     * 列表
     * $param string $token
     */
    public function leftMenu($token)
    {
        $uid = $this->uidByToken($token);
        $res = $this->getChildList($uid);
        return $this->toback(0, '', $res);
    }

    //获取子菜单
    public function getChildList($uid, $pid = 0)
    {
        $arys = [];
        $db = $this->dbConn($this->xmMenu)
            ->where('is_show', 1)
            ->where('type', 2)
            ->where('uid', $uid)
            ->where('pid', $pid);
        $res = $db->orderBy('sort', 'desc')
            ->get();
        if (!count($res)) {
            return $arys;
        }
        foreach ($res as $v) {
            $v = (array) $v;
            $arys[] = [
                'id' => $v['id'],
                'title' => $v['title'],
                'type' => $v['type'],
                'icon' => $v['icon'],
                'link' => $v['link'] ? $v['link'] : '',
                'pid' => $v['pid'],
                'childList' => $this->getChildList($uid, $v['id']),
            ];
        }
        return $arys;
    }

    /**
     * 后台功能列表
     * @param int $type 1前台，2后台
     */
    public function getIndex($token, $type)
    {
        $uid = $this->uidByToken($token);
        $db = $this->dbConn($this->xmMenu)->where('pid', 0);
        if ($type) {
            $db = $db->where('type', $type);
        }
        if ($uid) {
            $db = $db->where('uid', $uid);
        }
        $total = $db->count();
        $res = $db->select('id', 'type', 'title', 'link', 'pid')->get();
        if (!count($res)) {
            return $this->toback(-2, '没有菜单');
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $childArys = [];
            $db2 = $this->dbConn($this->xmMenu)->where('pid', $v['id']);
            if ($type) {
                $db2 = $db2->where('type', $type);
            }
            if ($uid) {
                $db2 = $db2->where('uid', $uid);
            }
            $childOnes = $db2->select('id', 'type', 'title', 'link', 'pid')->get();
            $total ++;
            if (count($childOnes)) {
                foreach ($childOnes as $vv) {
                    $vv = (array) $vv;
                    $childArys[] = [
                        'id' => $vv['id'],
                        'type' => $vv['type'],
                        'title' => $vv['title'],
                        'link' => $vv['link'],
                        'pid' => $vv['pid'],
                        'pname' => $v['title'],
                    ];
                    $total ++;
                }
            }
            $arys[] = [
                'id' => $v['id'],
                'type' => $v['type'],
                'title' => $v['title'],
                'link' => $v['link'],
                'pid' => 0,
                'pname' => '-',
                'childs' => $childArys,
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

    //后台功能一个
    public function getOne($id)
    {
        $res = (array) $this->dbConn($this->xmMenu)
            ->select('id', 'title', 'link', 'pid')
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '没有菜单');
        }
        return $this->toback(0, '', $res);
    }

    /**
     * 保存更新
     * @param int $id
     * @param array $pas
     */
    public function modify($id, $pas)
    {
        $res = (array) $this->dbConn($this->xmMenu)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, 'no data');
        }
        $pas['uptime'] = toDate();
        $up = $this->dbConn($this->xmMenu)->where('id', $id)->update($pas);
        if (!$up) {
            return $this->toback(-2, 'error');
        }
        return $this->toback();
    }
}